"use strict";

const path = require("path");
const expect = require("chai").expect;
const ValidationService = require(path.join(__dirname,
                                             "..",
                                             "..",
                                             "src",
                                             "services",
                                            "ValidationService.js"));

/** @test {ValidationService} */
describe("ValidationService", function() {
  let subject = new ValidationService();

  /** @test {ValidationService#constructor} */
  it("#constructor", function() {
    expect(subject).to.be.instanceof(ValidationService);
    expect(subject.properties).to.be.an("array");
  });

  /** @test {ValidationService#register} */
  describe("#register", function() {
    it("should return true if rules have the needed properties", function() {
      let validRules;
      let expectValid;
      validRules = {
        username: {
          name: "username",
          type: "string",
          required: true
        }
      };
      expectValid = subject.register(validRules);

      expect(expectValid).to.equal(true);
    });

    it("should return false if rules have not the needed properties", function() {
      let unvalidRules;
      let expectUnvalid;
      unvalidRules = {
        username: {
          name: "username"
        }
      };
      expectUnvalid = subject.register(unvalidRules);

      expect(expectUnvalid).to.equal(false);
    });
  });

  /** @test {ValidationService#execute} */
  describe("#execute", function() {
    it("should set status to success if no error", function() {
      let payload;
      let rule;
      let result;
      rule = {
        username: {
          name: "username",
          type: "string",
          required: true
        },
        email: {
          name: "email",
          type: "string",
          required: true
        }
      };
      payload = {username: "user", email: "john@doe.com"};
      subject.register(rule);
      subject.execute(payload);

      expect(subject.status.code).to.equal(200);
    });

    describe("should set status to success if an error found", function() {
      it("error: required", function() {
        let payload;
        let rule;
        rule = {
          username: {
            name: "username",
            type: "string",
            required: true
          }
        };
        payload = {email: "john@doe.com"};
        subject.register(rule);
        subject.execute(payload);

        expect(subject.status.code).to.equal(500);
      });
      it("error: type", function() {
        let rule = {
          username: {
            name: "username",
            type: "string",
            required: false
          }
        };
        let payload = {username: 42};
        subject.register(rule);
        subject.execute(payload);

        expect(subject.status.code).to.equal(500);

      });
      it("error: name", function() {
        let rule = {
          username: {
            name: "name",
            type: "string",
            required: true
          }
        };
        let payload = {username: "john"};
        subject.register(rule);
        subject.execute(payload);

        expect(subject.status.code).to.equal(500);
      });
    });
  });
});
